package com.module.overview.ui.readyCountdown

import android.animation.Animator
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.helper.RouterHelper
import com.common.app.utls.TimeUtils
import com.common.app.utls.TimeUtils.removeMilliseconds
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.yes
import com.common.base.utils.StatusBarUtils
import com.module.ble.listener.CusSportMonitorDataExpandListener
import com.module.ble.utils.AizoSDKUtil
import com.module.ble.utils.AizoSDKUtil.addSportMonitorDataExpandListener
import com.module.ble.utils.AizoSDKUtil.removeSportMonitorDataExpandListener
import com.module.fitness.utils.ExerciseType
import com.module.fitness.utils.getEnumToExType
import com.module.overview.databinding.OverviewActivityReadyCountdownBinding

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/4/7
 * <p>
 * 描述：运动预备倒计时页面
 * <p>
 * 修订历史：
 */
@Route(path = RouterHub.ROUTER_OVERVIEW_READY_COUNTDOWN)
class ReadyCountdownActivity : AppActivity<OverviewActivityReadyCountdownBinding, ReadyCountdownViewModel>(),
    CusSportMonitorDataExpandListener {

    override fun getViewModel(): Class<ReadyCountdownViewModel> = ReadyCountdownViewModel::class.java

    override fun getViewContentBinding() = OverviewActivityReadyCountdownBinding.inflate(layoutInflater)

    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var mType :ExerciseType? = ExerciseType.RUNNING_IN

    var curSportId : Long = -1//运动ID

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)
//        startSport()
    }

    override fun initEvents() {
        super.initEvents()
        addSportMonitorDataExpandListener(this)
//        mViewContentBinding.animCountdown.apply {
//
//            setTextColor(getColor(R.color.text_content))// 设置文本颜色为红色
//            setMaxScale(2.3f)// 设置最大放大倍数
//            setMinAlpha(0.1f)// 设置最小透明度为0.1（更透明）
//            setTextSize(dpDimenPx(R.dimen.dp_260))
////            setBold(true)
//            setOnFinishedListener {
//                (curSportId > 0).yes {
//                    val bundle = Bundle()
//                    bundle.putParcelable(KeyBundle.BUNDLE_KEY, mType)
//                    bundle.putLong(KeyBundle.EXPAND_DATA, curSportId)
//                    startActivity<DuringExerciseActivity>(bundle)
//                    Log.e("ReadyCountdownActivity", "YQ: SSSSSS: startActivity<DuringExerciseActivity>(bundle)")
//                }.otherwise {
//                    startSport()
//                    Log.e("ReadyCountdownActivity", "YQ: SSSSSS: startActivity<DuringExerciseActivity>(bundle) otherwise")
//                }
//            }
//        }

        mViewContentBinding.animCountdown.addAnimatorListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(p0: Animator) {
                Log.e("$TAG---SOU---LottieAnimationView", "onAnimationStart:")
            }

            override fun onAnimationEnd(p0: Animator) {
                Log.e("$TAG---SOU---LottieAnimationView", "onAnimationEnd:")
                mViewContentBinding.animCountdown.removeAllAnimatorListeners()

                (curSportId > 0).yes {
                    RouterHelper.navigationPostcard(RouterHub.ROUTER_OVERVIEW_DURING_EXERCISE)
                        .withParcelable(KeyBundle.BUNDLE_KEY, mType)
                        .withLong(KeyBundle.EXPAND_DATA, curSportId)
                        .navigation()
                    Log.e("ReadyCountdownActivity", "YQ: SSSSSS: startActivity<DuringExerciseActivity>(bundle)")
                }.otherwise {
                    startSport()
                    Log.e("ReadyCountdownActivity", "YQ: SSSSSS: startActivity<DuringExerciseActivity>(bundle) otherwise")
                }
            }

            override fun onAnimationCancel(p0: Animator) {
                Log.e("$TAG---SOU---LottieAnimationView", "onAnimationCancel:")
                mViewContentBinding.animCountdown.removeAllAnimatorListeners()
            }

            override fun onAnimationRepeat(p0: Animator) {
                Log.e("$TAG---SOU---LottieAnimationView", "onAnimationRepeat:")
            }

        })
        mViewContentBinding.animCountdown.addAnimatorPauseListener(object :
            Animator.AnimatorPauseListener {
            override fun onAnimationPause(p0: Animator) {
                Log.e("$TAG---SOU---LottieAnimationView", "onAnimationPause:")
            }

            override fun onAnimationResume(p0: Animator) {
                Log.e("$TAG---SOU---LottieAnimationView", "onAnimationResume:")
            }

        })
    }

    override fun lazyInitData() {
        super.lazyInitData()
        mViewContentBinding.animCountdown.playAnimation()
    }

    override fun onDestroy() {
        removeSportMonitorDataExpandListener(this)
        super.onDestroy()
    }

    override fun sportStart(result: Boolean, sportId : Long, ringFirm: String) {
        dismissLoadingDialog()
        Log.e("ReadyCountdownActivity-sportStart", "YQ: SSSSSS: sportStart ${result}-------${sportId}")
        result.yes {
            curSportId = sportId
        }
    }

    override fun sportPause(result: Boolean) {
    }

    override fun sportResume(result: Boolean) {
    }

    override fun sportStop(result: Boolean) {
    }

    override fun sportInfo(
        type: Int,
        timestamp: Long,
        step: Int,
        distance: Int,
        cal: Int,
        cursleeptime: Int,
        totalrunningtime: Int,
        teptime: Int
    ) {
    }

//    override fun onBackPressedSupport() {
//    }

    fun startSport(){
        mType?.let {
            AizoSDKUtil.setSportStart(getEnumToExType(it),removeMilliseconds(TimeUtils.getTimeDateLong()))
        }
    }
}